﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LaughingDog.GameObjects;
using Microsoft.Xna.Framework;
using FarseerGames.FarseerPhysics.Factories;
using FarseerGames.FarseerPhysics.Dynamics.Joints;
using LaughingDog.Managers;

namespace LaughingDog.Components
{
    public class PinJoint : Joint
    {
        private FarseerGames.FarseerPhysics.Dynamics.Joints.PinJoint mPinJoint;
        private RigidBody mConnectedBody = null;

        /// <summary>
        /// Gets or sets the desired distance between the two anchors. If no distance is specified, the offset between the bodies will be the target distance.
        /// </summary>
        public Single TargetDistance { get { return mPinJoint.TargetDistance; } set { mPinJoint.TargetDistance = value; } }

        /// <summary>
        /// The second RigidBody this Spring is attached to. The first RigidBody is attached to the GameObject that the Joint is attached to.
        /// </summary>
        public RigidBody ConnectedBody { get { return mConnectedBody; } set { mConnectedBody = value; } }

        /// <summary>
        /// Gets or sets the local anchor for the first RigidBody.
        /// </summary>
        public Vector2 Anchor1 { get { return mPinJoint.Anchor1; } set { mPinJoint.Anchor1 = value; } }

        /// <summary>
        /// Gets or sets the local anchor for the second RigidBody.
        /// </summary>
        public Vector2 Anchor2 { get { return mPinJoint.Anchor2; } set { mPinJoint.Anchor2 = value; } }

        /// <summary>
        /// Gets the world anchor point for the first RigidBody.
        /// </summary>
        public Vector2 WorldAnchor1 { get { return mPinJoint.WorldAnchor1; } }

        /// <summary>
        /// Gets the world anchor point for the second RigidBody.
        /// </summary>
        public Vector2 WorldAnchor2 { get { return mPinJoint.WorldAnchor2; } }

        /// <summary>
        /// Creates an instance of a PinJoint component.
        /// </summary>
        /// <param name="gameObject">The GameObject this component is attached to.</param>
        public PinJoint(GameObject gameObject)
            : base(gameObject)
        {
            mPinJoint = new FarseerGames.FarseerPhysics.Dynamics.Joints.PinJoint();
            mJoint = mPinJoint;
        }

        /// <summary>
        /// Create the Joint after setting initial settings such as the ConnectedBody.
        /// </summary>
        public override void Create()
        {            
            mPinJoint = JointFactory.Instance.CreatePinJoint(PhysicsManager.Instance.Simulator, GameObject.RigidBody.Body, Anchor1, ConnectedBody.Body, Anchor2);
            mJoint = mPinJoint;
        }
    }
}
